//
//  JOLoginViewController.m
//  taxin_song
//
//  Created by song on 16/1/16.
//  Copyright © 2016年 song. All rights reserved.
//

#import "JOLoginViewController.h"
#import "JOLogHeader.h"
#import "JONameTextField.h"
#import "JOPassTextField.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
#import "JOMainViewController.h"
#import "JORegisterViewController.h"

@interface JOLoginViewController ()

@property(strong ,nonatomic) JOLogHeader *headview;

@property(strong ,nonatomic) UIImageView *nameimg;

@property(strong , nonatomic ) UIImageView *passimg;

@property(strong, nonatomic ) UITextField *nameField;

@property(strong , nonatomic ) UITextField *passField;

@end

@implementation JOLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"red");
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self setupCompant];

    [self setupNavgation];
    
    
    
}

-(void)setupCompant
{
    
    float space = 30;
    
    int Main_W = self.view.frame.size.width;
    int Main_H = self.view.frame.size.height;
   
    _headview =[JOLogHeader buttonWithType:UIButtonTypeCustom];
    [_headview setBackgroundImage:[UIImage imageNamed:@"login_logo_blue"] forState:UIControlStateNormal];
    _headview.frame = CGRectMake(0, 60, self.view.frame.size.width, 200);
    [_headview setImage:[UIImage imageNamed:@"login_logo"] forState:UIControlStateNormal];
  
    [self.view addSubview:_headview];
    
    
    JONameTextField *nameField = [[JONameTextField alloc] init];
    nameField.imgname =@"login_password";
    nameField.holder =@"请输入帐号";
    nameField.frame = CGRectMake(0, CGRectGetMaxY(_headview.frame) +20 , self.view.frame.size.width, 50);
    [self.view addSubview:nameField];
    _nameField = nameField;
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nameField.frame), Main_W, 1) ];
    [line setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:line];
    
    JOPassTextField  *passField = [[JOPassTextField alloc] init];
    passField.frame = CGRectMake(0, CGRectGetMaxY(line.frame) , self.view.frame.size.width, 50);
    [self.view addSubview:passField];
    _passField = passField;
    
    UIButton *logbtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [logbtn setImage:[UIImage imageNamed:@"login_load_btn"] forState:UIControlStateNormal];
    [logbtn addTarget:self action:@selector(logbtnClick) forControlEvents:UIControlEventTouchUpInside];
    int logbtn_w = 200;
    int logbtn_h = 30;
    int logbtn_x = (Main_W - logbtn_w)/2;
    int logbtn_y = CGRectGetMaxY(passField.frame)+10;
    logbtn.frame =CGRectMake(logbtn_x, logbtn_y, logbtn_w, logbtn_h);
    [self.view addSubview:logbtn];
    
    
    UIButton *regbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [regbtn setTitle:@"注册" forState:UIControlStateNormal];
    [regbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [regbtn addTarget:self action:@selector(regbtnClick) forControlEvents:UIControlEventTouchUpInside];
    regbtn.frame = CGRectMake(10, CGRectGetMaxY(logbtn.frame)+20, 60, 30);
    [self.view addSubview:regbtn];
    
    
  
}



-(void)setupNavgation{
    self.navigationItem.title = @"登录";
    
    UIButton *showbtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    [showbtn setTitle:@"浏览" forState:UIControlStateNormal];
    [showbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    showbtn.frame = CGRectMake(0, 0, 64, 30);
    UIBarButtonItem  *barrightbtn = [[UIBarButtonItem alloc] initWithCustomView:showbtn];
    self.navigationItem.rightBarButtonItem =barrightbtn;
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:56 green:156 blue:252 alpha:0 ];
}




-(void)logbtnClick
{
    NSString *name = _nameField.text;
    NSString *pass = _passField.text;

    if((name==nil || [name isEqualToString:@""]) || pass ==nil ||[pass isEqualToString:@""] )
    {
       
        [SVProgressHUD showErrorWithStatus:@" 帐号或密码错误 "];
    }else{
        
        
        AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
        manager.responseSerializer =  [AFJSONResponseSerializer serializer];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:name forKey:@"name"];
        [dic setObject:pass forKey:@"pass"];
        NSString *url =@"http://localhost/song/login.php";
        [manager POST:url parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull data) {
            NSDictionary *dic = data;
            NSString *status = dic[@"status"];
            if([status isEqualToString:@"OK"])
            {
                JOMainViewController *mainCtl = [[JOMainViewController alloc]init];
                UIWindow *window = self.view.window;
                window.rootViewController = mainCtl;
                [window makeKeyAndVisible];
                
            }else{
                [SVProgressHUD showErrorWithStatus:@"帐号或密码错误"];
            }
            
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            [SVProgressHUD showErrorWithStatus:@"网络错误"];
        }];
        
        
    }
    
}

-(void)regbtnClick{
    
    JORegisterViewController *regctl = [[JORegisterViewController alloc] init];
    [self.navigationController pushViewController:regctl animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
